import { defineEventHandler, getQuery } from 'h3';

export default defineEventHandler(async (event) => {
  const query = getQuery(event);

  // 资源市场列表数据
  const marketListings = [
    {
      id: '1',
      resourceId: '1',
      resourceName: '豪华酒店主楼',
      resourceType: 'PHYSICAL',
      businessType: 'HOTEL',
      title: '豪华酒店主楼 - 25层建筑，200间客房',
      description:
        '位于市中心的豪华酒店主楼，25层建筑，200间客房，配备完善的设施和服务',
      price: {
        currency: 'CNY',
        basePrice: 8_000_000,
        currentPrice: 7_200_000,
        discount: 0.1,
        unit: 'month',
        negotiable: true,
      },
      seller: {
        id: 'seller_1',
        name: '酒店管理集团',
        rating: 4.8,
        verified: true,
        location: '北京市朝阳区',
        avatar: '/images/sellers/seller-1.jpg',
        description: '专业的酒店管理集团，拥有20年经验',
      },
      status: 'active',
      category: 'hotel',
      tags: ['豪华酒店', '市中心', '25层', '200间客房'],
      features: [
        '25层建筑',
        '200间客房',
        '大堂餐厅',
        '会议室',
        '健身房',
        '游泳池',
        '停车场',
      ],
      images: [
        '/images/hotel-building-1.jpg',
        '/images/hotel-building-2.jpg',
        '/images/hotel-building-3.jpg',
      ],
      specifications: {
        buildingArea: '15000平方米',
        floors: 25,
        rooms: 200,
        parkingSpaces: 100,
        constructionYear: 2020,
        facilities: ['大堂', '餐厅', '会议室', '健身房', '游泳池'],
      },
      availability: {
        startDate: '2024-02-01',
        endDate: '2024-12-31',
        minDuration: 1,
        maxDuration: 12,
        currentBookings: 0,
        totalCapacity: 1,
      },
      ratings: {
        average: 4.7,
        total: 156,
        breakdown: {
          5: 120,
          4: 25,
          3: 8,
          2: 2,
          1: 1,
        },
      },
      createdAt: '2024-01-15T10:00:00Z',
      updatedAt: '2024-01-20T14:30:00Z',
      views: 1250,
      favorites: 89,
      isHot: true,
      isNew: false,
    },
    {
      id: '2',
      resourceId: '3',
      resourceName: '行政套房1501',
      resourceType: 'PHYSICAL',
      businessType: 'HOTEL',
      title: '豪华行政套房1501 - 80平米，城市景观',
      description:
        '豪华行政套房，80平米，配备全套设施，城市景观，适合商务和休闲',
      price: {
        currency: 'CNY',
        basePrice: 1500,
        currentPrice: 1200,
        discount: 0.2,
        unit: 'night',
        negotiable: false,
      },
      seller: {
        id: 'seller_1',
        name: '酒店管理集团',
        rating: 4.8,
        verified: true,
        location: '北京市朝阳区',
        avatar: '/images/sellers/seller-1.jpg',
        description: '专业的酒店管理集团，拥有20年经验',
      },
      status: 'active',
      category: 'hotel',
      tags: ['行政套房', '豪华', '城市景观', '80平米'],
      features: [
        '80平米',
        '城市景观',
        '大床',
        '客厅',
        '书房',
        '衣帽间',
        '浴室',
        '阳台',
      ],
      images: [
        '/images/suite-1501-1.jpg',
        '/images/suite-1501-2.jpg',
        '/images/suite-1501-3.jpg',
      ],
      specifications: {
        area: '80平方米',
        bedCount: 2,
        floor: 15,
        orientation: '南',
        facilities: ['大床', '客厅', '书房', '衣帽间', '浴室', '阳台'],
        amenities: ['免费WiFi', '迷你吧', '保险箱', '浴袍', '拖鞋'],
      },
      availability: {
        startDate: '2024-01-25',
        endDate: '2024-12-31',
        minDuration: 1,
        maxDuration: 30,
        currentBookings: 0,
        totalCapacity: 1,
      },
      ratings: {
        average: 4.9,
        total: 89,
        breakdown: {
          5: 75,
          4: 12,
          3: 2,
          2: 0,
          1: 0,
        },
      },
      createdAt: '2024-01-18T09:30:00Z',
      updatedAt: '2024-01-21T16:45:00Z',
      views: 890,
      favorites: 67,
      isHot: false,
      isNew: true,
    },
    {
      id: '3',
      resourceId: '10',
      resourceName: '酒店管理系统',
      resourceType: 'DIGITAL',
      businessType: 'HOTEL',
      title: '酒店管理系统 - SaaS服务，企业级',
      description: '企业级酒店管理系统，SaaS服务，支持多酒店管理，功能完善',
      price: {
        currency: 'CNY',
        basePrice: 50_000,
        currentPrice: 45_000,
        discount: 0.1,
        unit: 'month',
        negotiable: true,
      },
      seller: {
        id: 'seller_2',
        name: '酒店科技公司',
        rating: 4.6,
        verified: true,
        location: '上海市浦东新区',
        avatar: '/images/sellers/seller-2.jpg',
        description: '专注于酒店行业的技术解决方案提供商',
      },
      status: 'active',
      category: 'software',
      tags: ['酒店管理', 'SaaS', '企业级', '多酒店'],
      features: [
        '预订管理',
        '客房管理',
        '财务管理',
        '报表分析',
        '多酒店支持',
        'API集成',
        '移动端支持',
      ],
      images: ['/images/hotel-system-1.jpg', '/images/hotel-system-2.jpg'],
      specifications: {
        systemType: 'PMS',
        version: 'v2.1.0',
        technologyStack: ['Vue3', 'Node.js', 'MySQL'],
        deployment: 'cloud',
        modules: ['预订管理', '客房管理', '财务管理', '报表分析'],
        integrations: ['OTA', '支付系统', '门锁系统'],
      },
      availability: {
        startDate: '2024-01-01',
        endDate: '2024-12-31',
        minDuration: 1,
        maxDuration: 12,
        currentBookings: 15,
        totalCapacity: 100,
      },
      ratings: {
        average: 4.5,
        total: 234,
        breakdown: {
          5: 180,
          4: 40,
          3: 10,
          2: 3,
          1: 1,
        },
      },
      createdAt: '2024-01-10T14:20:00Z',
      updatedAt: '2024-01-19T11:15:00Z',
      views: 2100,
      favorites: 156,
      isHot: true,
      isNew: false,
    },
    {
      id: '4',
      resourceId: '11',
      resourceName: '酒店管理团队',
      resourceType: 'HUMAN',
      businessType: 'HOTEL',
      title: '专业酒店管理团队 - 15人，8年经验',
      description:
        '专业酒店管理团队，15人，平均8年经验，提供完整的酒店管理服务',
      price: {
        currency: 'CNY',
        basePrice: 150_000,
        currentPrice: 135_000,
        discount: 0.1,
        unit: 'month',
        negotiable: true,
      },
      seller: {
        id: 'seller_3',
        name: '酒店管理咨询公司',
        rating: 4.7,
        verified: true,
        location: '广州市天河区',
        avatar: '/images/sellers/seller-3.jpg',
        description: '专业的酒店管理咨询服务公司',
      },
      status: 'active',
      category: 'service',
      tags: ['管理团队', '专业', '8年经验', '15人'],
      features: [
        '前厅部管理',
        '客房部管理',
        '餐饮部管理',
        '财务部管理',
        '工程部管理',
        '质量管理',
        '培训服务',
      ],
      images: [
        '/images/management-team-1.jpg',
        '/images/management-team-2.jpg',
      ],
      specifications: {
        teamSize: 15,
        experienceYears: 8,
        departments: ['前厅部', '客房部', '餐饮部', '财务部', '工程部'],
        certifications: ['酒店管理师', 'ISO9001内审员'],
        managementLevel: 'senior',
        languages: ['中文', '英文'],
      },
      availability: {
        startDate: '2024-02-01',
        endDate: '2024-12-31',
        minDuration: 3,
        maxDuration: 12,
        currentBookings: 2,
        totalCapacity: 5,
      },
      ratings: {
        average: 4.8,
        total: 67,
        breakdown: {
          5: 55,
          4: 10,
          3: 2,
          2: 0,
          1: 0,
        },
      },
      createdAt: '2024-01-12T16:30:00Z',
      updatedAt: '2024-01-20T13:45:00Z',
      views: 780,
      favorites: 45,
      isHot: false,
      isNew: false,
    },
    {
      id: '5',
      resourceId: '7',
      resourceName: '中餐厅',
      resourceType: 'PHYSICAL',
      businessType: 'RESTAURANT',
      title: '中餐厅 - 300平米，传统中式美食',
      description:
        '传统中餐厅，300平米，提供正宗中式美食，适合商务宴请和家庭聚餐',
      price: {
        currency: 'CNY',
        basePrice: 50_000,
        currentPrice: 45_000,
        discount: 0.1,
        unit: 'month',
        negotiable: true,
      },
      seller: {
        id: 'seller_4',
        name: '餐饮管理公司',
        rating: 4.4,
        verified: true,
        location: '深圳市南山区',
        avatar: '/images/sellers/seller-4.jpg',
        description: '专业的餐饮管理和运营公司',
      },
      status: 'active',
      category: 'restaurant',
      tags: ['中餐厅', '传统', '300平米', '商务宴请'],
      features: [
        '300平米',
        '传统中式',
        '厨房设备',
        '用餐区',
        '包间',
        '收银台',
        '停车位',
      ],
      images: [
        '/images/chinese-restaurant-1.jpg',
        '/images/chinese-restaurant-2.jpg',
      ],
      specifications: {
        area: '300平方米',
        maxCapacity: 120,
        cuisineType: '中式',
        facilities: ['厨房', '用餐区', '包间', '收银台'],
        parkingSpaces: 20,
        serviceQuality: '四星级',
      },
      availability: {
        startDate: '2024-02-01',
        endDate: '2024-12-31',
        minDuration: 6,
        maxDuration: 12,
        currentBookings: 0,
        totalCapacity: 1,
      },
      ratings: {
        average: 4.3,
        total: 123,
        breakdown: {
          5: 85,
          4: 25,
          3: 10,
          2: 2,
          1: 1,
        },
      },
      createdAt: '2024-01-16T11:20:00Z',
      updatedAt: '2024-01-21T09:30:00Z',
      views: 650,
      favorites: 34,
      isHot: false,
      isNew: false,
    },
  ];

  // 过滤逻辑
  let filteredListings = marketListings;

  if (query.keyword) {
    const keyword = query.keyword as string;
    filteredListings = filteredListings.filter(
      (listing) =>
        listing.title.includes(keyword) ||
        listing.description.includes(keyword) ||
        listing.tags.some((tag) => tag.includes(keyword)),
    );
  }

  if (query.category) {
    filteredListings = filteredListings.filter(
      (listing) => listing.category === query.category,
    );
  }

  if (query.resourceType) {
    filteredListings = filteredListings.filter(
      (listing) => listing.resourceType === query.resourceType,
    );
  }

  if (query.businessType) {
    filteredListings = filteredListings.filter(
      (listing) => listing.businessType === query.businessType,
    );
  }

  if (query.minPrice) {
    const minPrice = Number.parseInt(query.minPrice as string);
    filteredListings = filteredListings.filter(
      (listing) => listing.price.currentPrice >= minPrice,
    );
  }

  if (query.maxPrice) {
    const maxPrice = Number.parseInt(query.maxPrice as string);
    filteredListings = filteredListings.filter(
      (listing) => listing.price.currentPrice <= maxPrice,
    );
  }

  if (query.status) {
    filteredListings = filteredListings.filter(
      (listing) => listing.status === query.status,
    );
  }

  if (query.isHot === 'true') {
    filteredListings = filteredListings.filter((listing) => listing.isHot);
  }

  if (query.isNew === 'true') {
    filteredListings = filteredListings.filter((listing) => listing.isNew);
  }

  // 排序
  const sortBy = (query.sortBy as string) || 'createdAt';
  const sortOrder = (query.sortOrder as string) || 'desc';

  filteredListings.sort((a, b) => {
    let aValue, bValue;

    switch (sortBy) {
      case 'favorites': {
        aValue = a.favorites;
        bValue = b.favorites;
        break;
      }
      case 'price': {
        aValue = a.price.currentPrice;
        bValue = b.price.currentPrice;
        break;
      }
      case 'rating': {
        aValue = a.ratings.average;
        bValue = b.ratings.average;
        break;
      }
      case 'views': {
        aValue = a.views;
        bValue = b.views;
        break;
      }
      case 'createdAt':
      default: {
        aValue = new Date(a.createdAt).getTime();
        bValue = new Date(b.createdAt).getTime();
        break;
      }
    }

    return sortOrder === 'asc' ? aValue - bValue : bValue - aValue;
  });

  // 分页
  const page = Number.parseInt(query.page as string) || 1;
  const pageSize = Number.parseInt(query.pageSize as string) || 10;
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  const items = filteredListings.slice(start, end);

  return {
    code: 0,
    data: {
      items,
      total: filteredListings.length,
      page,
      pageSize,
    },
    msg: '获取资源市场列表成功',
  };
});
